﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using PracticeAzure.Website.Shared;
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;

namespace PracticeAzure.Website.Data
{
    public class Announcement
    {
        public long ID { get; set; }

        [Required(ErrorMessage = "请输入消息正文")]
        public string Body { get; set; }

        public DateTime CreatedOn { get; set; }

        public MvcHtmlString RenderedBody
        {
            get
            {
                var html = Body;
                html = RenderEngine.Current.Render(html);
                return MvcHtmlString.Create(html);
            }
        }

        public void Save()
        {
            using (var conn = SqlHelperLite.CreateSqlConnection())
            {
                var cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "CreateAnnouncement";
                cmd.Parameters.AddReturnValue();
                cmd.Parameters.Add(new SqlParameter("@body", Body));
                conn.Open();
                cmd.ExecuteNonQuery();
                ID = (long)(int)cmd.GetReturnValue();
            }
        }

        public static IEnumerable<Announcement> List()
        {
            var announcements = new List<Announcement>();
            using (var conn = SqlHelperLite.CreateSqlConnection())
            {
                var cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "ListAnnouncement";
                conn.Open();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        announcements.Add(new Announcement()
                        {
                            ID = (long)reader["ID"],
                            Body = (string)reader["Body"],
                            CreatedOn = (DateTime)reader["CreatedOn"]
                        });
                    }
                }
            }
            return announcements;
        }

        public static IEnumerable<Announcement> Recent()
        {
            var announcements = new List<Announcement>();
            using (var conn = SqlHelperLite.CreateSqlConnection())
            {
                var cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "RecentAnnouncement";
                conn.Open();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        announcements.Add(new Announcement()
                        {
                            ID = (long)reader["ID"],
                            Body = (string)reader["Body"],
                            CreatedOn = (DateTime)reader["CreatedOn"]
                        });
                    }
                }
            }
            return announcements;
        }
    }
}